﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management.Automation;

using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;

namespace Mystery.SharePoint.PowerShell
{
	[Cmdlet("New", "SPMServerDirectory")]
	public class NewServerDirectoryCmdlet : SPCmdlet
	{
		/// <summary>
		/// The path that should be created on each server
		/// </summary>
		[Parameter(Mandatory=true)]
		public string Path { get; set; }
        /// <summary>
        /// The list of servers to apply this operation on 
        /// </summary>
        [Parameter(Mandatory = true)]
        public string[] Servers { get; set; }

		protected override void InternalProcessRecord()
		{
            foreach (string server in this.Servers)
			{				
				string path = Utils.CreateUncPath(server, this.Path);

				try
				{
					if (Directory.Exists(path) == false)
					{
						base.Host.UI.WriteLine(string.Format(null, "Creating: {0}", path));
						Directory.CreateDirectory(path);
					}
				}
				catch (IOException ex)
				{
					base.WriteError(ex, ErrorCategory.ResourceUnavailable, server);
				}		
			}
		}
	}
}
